const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

const path = require('path')

module.exports = {
  // eslint-loader 是否在保存的时候检查
  lintOnSave: process.env.NODE_ENV === 'development',
  // webpack配置
  configureWebpack: {
    resolve: {
      alias: {
        'assets': path.resolve('./src/assets'),
        'components': path.resolve('./src/components'),
        'utils': path.resolve('./src/utils'),
        'styles': path.resolve('./src/styles'),
        'views': path.resolve('./src/views')
      }
    }
  },
  // 生产环境是否生成 sourceMap 文件
  productionSourceMap: false,
  css: {
    loaderOptions: {
      less: {
        modifyVars: {
          'primary-color': '#1DA57A',
          'link-color': '#1DA57A',
          'border-radius-base': '2px'
        },
        javascriptEnabled: true
      }
    }
  },
  // 服务配置
  devServer: {
    open: true,
    hot: true,
    overlay: {
      warnings: true,
      errors: true
    },
    port: 9521,
    https: false,
    hotOnly: false,
    // 配置多个代理
    proxy: {
      // process.env.VUE_APP_BASE_API
      '/base': {
        target: 'http://127.0.0.1:9521',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/base': ''
        }
      }
    }
  },
  chainWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      // 图片压缩
      config.module
        .rule('images')
        .test(/\.(png|jpe?g|gif|svg)(\?.*)?$/)
        .use('img-loader')
        .loader('img-loader').options({
          plugins: [
            require('imagemin-jpegtran')(),
            require('imagemin-pngquant')({
              quality: [0.75, 0.85]
            })
          ]
        })
    }
    // 分析打包体积
    if (process.env.IS_ANALYZE) {
      config.plugin('webpack-report').use(BundleAnalyzerPlugin, [
        {
          analyzerMode: 'static'
        }
      ])
    }
  }
}
